package servlet;

import bean.Dog;
import bean.Person;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/test")
public class TestServlet  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        Dog dog = new Dog(1,"大黄");
//        Person person = new Person(1,"菊花",dog);
//
//        req.setAttribute("personData",person);

//        req.setAttribute("key","request");
//        HttpSession session = req.getSession();
//         session.setAttribute("key","session");
//        this.getServletContext().setAttribute("key","context");

        List<Person> list = new ArrayList<>();
        list.add(new Person(1,"小张",18,new Dog(1,"大黄")));
        list.add(new Person(2,"小李",15,new Dog(2,"大黑")));
        list.add(new Person(3,"小王",17,new Dog(3,"大红")));
        list.add(new Person(4,"小赵",20,new Dog(4,"大紫")));
        list.add(new Person(5,"小孙",21,new Dog(5,"大军")));

         req.setAttribute("persons",list);

        req.getRequestDispatcher("/showEl.jsp").forward(req,resp);

    }
}
